home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Mac OS SDK / Dev.CD Jul 96 SDK / Dev.CD Jul 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / Developer University / DUProjects / GraphicsBfr / Sources / Frame.cpp < prev    next >
Encoding:
Text File  |  1996-03-26  |  12.8 KB  |  441 lines  |  [TEXT/CWIE]

  1. //    Copyright © 1995 Apple Computer, Inc. All rights reserved.
  2. //    Release Version:    $ 1.0 d11 $
  3.  
  4. //=======================================================================
  5. #ifndef FRAME_H
  6. #include "Frame.h"
  7. #endif
  8.  
  9. #ifndef PART_H
  10. #include "Part.h"
  11. #endif
  12.  
  13. // ----- Framework Layer -----
  14. #ifndef FWUTIL_H
  15. #include "FWUtil.h"                // FW_CFacetContext, FW_Beep()
  16. #endif
  17.  
  18. #ifndef FWCONTXT_H
  19. #include "FWContxt.h"            // FW_CViewContext
  20. #endif
  21.  
  22. #ifndef FWPRHDLR_H
  23. #include "FWPrHdlr.h"            // FW_CPrintHandler
  24. #endif
  25.  
  26. // ----- OS Layer -----
  27. #ifndef FWMENU_H
  28. #include "FWMenu.h"                // FW_CMenuBar, etc.
  29. #endif
  30.  
  31. #ifndef FWEVENT_H
  32. #include "FWEvent.h"            // FW_CMenuEvent, FW_CMouseEvent
  33. #endif
  34.  
  35. #ifndef FWALERT_H
  36. #include "FWAlert.h"            // FW_CAlert
  37. #endif
  38.  
  39. #ifndef FWEVEDEF_H
  40. #include "FWEveDef.h"            // FW_BACKSPACE, etc.
  41. #endif
  42.  
  43. #ifndef FWCFMRES_H
  44. #include "FWCFMRes.h"            // FW_CSharedLibraryResourceFile
  45. #endif
  46.  
  47. #ifndef FWODGEOM_H
  48. #include "FWODGeom.h"            // FW_NewODShape
  49. #endif
  50.  
  51. #ifndef SLMIXCOS_H
  52. #include "SLMixOS.h"            // FW_Beep
  53. #endif
  54.  
  55. // ----- Graphic Includes -----
  56. #ifndef FWRECT_H
  57. #include <FWRect.h>                // FW_CRect
  58. #endif
  59.  
  60. #ifndef FWRECSHP_H
  61. #include "FWRecShp.h"            // FW_CRectShape
  62. #endif
  63.  
  64. #ifndef FWPOLY_H
  65. #include "FWPoly.h"                // FW_PPolygon
  66. #endif
  67.  
  68. #ifndef FWPOLYSH_H
  69. #include "FWPolySh.h"            // FW_CPolygonShape
  70. #endif
  71.  
  72. #ifndef FWOVLSHP_H
  73. #include "FWOvlShp.h"            // FW_COvalShape
  74. #endif
  75.  
  76. #ifndef FWPICSHP_H
  77. #include "FWPicShp.h"            // FW_PPicture, FW_CPictureShape
  78. #endif
  79.  
  80. #ifndef FWTXTSHP_H
  81. #include "FWTxtShp.h"            // FW_CTextShape
  82. #endif
  83.  
  84. #ifndef FWLINSHP_H
  85. #include "FWLinShp.h"            // FW_CLineShape
  86. #endif
  87.  
  88. #ifndef FWRGNSHP_H
  89. #include "FWRgnShp.h"            // FW_CRegionShape
  90. #endif
  91.  
  92. //========================================================================================
  93. #ifdef FW_BUILD_MAC
  94. #pragma segment GraphicsBfr
  95. #endif
  96.  
  97. //========================================================================================
  98. FW_DEFINE_AUTO(CGraphicsBfrFrame)
  99.  
  100. //========================================================================================
  101. CGraphicsBfrFrame::CGraphicsBfrFrame(Environment* ev, ODFrame* odFrame, 
  102.                                     FW_CPresentation* presentation, CGraphicsBfrPart* part)
  103.   : FW_CFrame(ev, odFrame, presentation, part),
  104.       fLargePictureShape(NULL),
  105.       fLineShape(NULL),
  106.       fRegionShape(NULL),
  107.       fPolygonShape(NULL),
  108.       fBitmap(1, 1, 0),
  109.       fBitmapShape(NULL),
  110.       fDisplayChanged(TRUE)
  111. {
  112.     fLargePictureShape = MyCreatePictureShape(ev);
  113.     fLineShape = MyCreateLineShape(ev);
  114.     fRegionShape = MyCreateRegionShape(ev);
  115.     fPolygonShape = MyCreatePolygonShape(ev);
  116.     this->MyUpdateBitmap(ev);
  117.     FW_END_CONSTRUCTOR
  118. }
  119.  
  120. //----------------------------------------------------------------------------------------
  121. CGraphicsBfrFrame::~CGraphicsBfrFrame()
  122. {
  123.     FW_START_DESTRUCTOR
  124.     delete fLargePictureShape;
  125.     delete fLineShape;
  126.     delete fRegionShape;
  127.     delete fPolygonShape;
  128.     delete fBitmapShape;
  129. }
  130.  
  131. //------------------------------------------------------------------------------
  132. FW_CPictureShape*
  133. CGraphicsBfrFrame::MyCreatePictureShape(Environment* ev)
  134. {
  135.     FW_CSharedLibraryResourceFile resFile(ev);    // resource from our shared library
  136.  
  137.     // draw large picture - use natural picture size
  138.     const short kMacPictID = 2001;
  139.     const short kCatPictID = 2002;
  140.     const short kOPFIconPictID = 2003;
  141.     FW_CPicture picture(resFile, kMacPictID);
  142.     picture.GetPictBounds(fLargePictRect);
  143.     return FW_NEW(FW_CPictureShape, (picture, fLargePictRect) );
  144. }
  145.  
  146. //------------------------------------------------------------------------------
  147. FW_CLineShape*
  148. CGraphicsBfrFrame::MyCreateLineShape(Environment* ev)
  149. {
  150.     // derived from FW_CAutoDestructObject
  151.     FW_CLineShape* shp = FW_NEW(FW_CLineShape,
  152.                         (FW_CPoint(FW_IntToFixed(200), FW_IntToFixed(70)),
  153.                          FW_CPoint(FW_IntToFixed(300), FW_IntToFixed(70)) ) );
  154.     shp->GetStyle().SetPenSize(FW_IntToFixed(5));
  155.     shp->GetInk().SetForeColor(FW_kRGBRed);
  156.     return shp;
  157. }
  158.  
  159. //------------------------------------------------------------------------------
  160. FW_CRegionShape*
  161. CGraphicsBfrFrame::MyCreateRegionShape(Environment* ev)
  162. {
  163.     FW_CRegionShape* shp = NULL;
  164. #ifdef FW_BUILD_MAC
  165.     // make an arrow QuickDraw region
  166.     RgnHandle rgnHdl = NewRgn();
  167.     OpenRgn();
  168.     MoveTo(00,10);    // start
  169.     LineTo(40,10);    // right
  170.     LineTo(40,00);    // up
  171.     LineTo(60,20);    // to point
  172.     LineTo(40,40);    // from point
  173.     LineTo(40,30);    // up
  174.     LineTo(00,30);    // left
  175.     LineTo(00,10);    // up
  176.     CloseRgn(rgnHdl);
  177.     // make OpenDoc shape
  178.     ODShape* odArrowShape = FW_NewODShape(ev, rgnHdl);
  179.     // make ODF shape
  180.     shp = FW_NEW(FW_CRegionShape, (odArrowShape, FW_kFill) );
  181.     shp->MoveShape(FW_IntToFixed(40), FW_IntToFixed(150));
  182.     shp->GetInk().SetForeColor(FW_kRGBRed);
  183. #endif
  184.     return shp;
  185. }
  186.  
  187. //------------------------------------------------------------------------------
  188. FW_CPolygonShape*
  189. CGraphicsBfrFrame::MyCreatePolygonShape(Environment* ev)
  190. {
  191.     FW_CPoint* points = new FW_CPoint[8];
  192.     points[0] = FW_CPoint(FW_IntToFixed(230), FW_IntToFixed(330));
  193.     points[1] = FW_CPoint(FW_IntToFixed(270), FW_IntToFixed(330));
  194.     points[2] = FW_CPoint(FW_IntToFixed(270), FW_IntToFixed(320));
  195.     points[3] = FW_CPoint(FW_IntToFixed(290), FW_IntToFixed(340));
  196.     points[4] = FW_CPoint(FW_IntToFixed(270), FW_IntToFixed(360));
  197.     points[5] = FW_CPoint(FW_IntToFixed(270), FW_IntToFixed(350));
  198.     points[6] = FW_CPoint(FW_IntToFixed(230), FW_IntToFixed(350));
  199.     points[7] = FW_CPoint(FW_IntToFixed(230), FW_IntToFixed(330));
  200.     FW_CPolygon poly(8, points);
  201.     delete [] points;
  202.     FW_Boolean kAutoCloseFrame    = true;             
  203.     FW_CPolygonShape* shape = FW_NEW(FW_CPolygonShape, (poly, FW_kFill, kAutoCloseFrame));
  204.     return shape;
  205. }
  206.  
  207. //----------------------------------------------------------------------------------------
  208. void 
  209. CGraphicsBfrFrame::Draw(Environment *ev, ODFacet* odFacet, ODShape* invalidShape)    // Override
  210. {
  211.     ODCanvas* canvas = odFacet->GetCanvas(ev);
  212.     FW_Boolean dynamicCanvas = canvas->IsDynamic(ev);
  213.     FW_CViewContext vContext(ev, this, odFacet, invalidShape);
  214.     if (dynamicCanvas) {                    // not printing
  215.         this->MyDrawToBitmap(ev, invalidShape);
  216.         fBitmapShape->Render(vContext);
  217.         }
  218.     else
  219.         this->MyDrawAllStuff(ev, vContext);    // for printing    
  220. }
  221.  
  222. //------------------------------------------------------------------------------
  223. void 
  224. CGraphicsBfrFrame::MyDrawBackground(Environment* ev, FW_CGraphicContext& gc)
  225. {
  226.     FW_CRect box = this->GetBounds(ev);    
  227.     FW_CRectShape::RenderRect(gc, box, FW_kFill, FW_kRGBLightGray);
  228. }
  229.  
  230. //----------------------------------------------------------------------------------------
  231. void 
  232. CGraphicsBfrFrame::MyDrawAllStuff(Environment* ev, FW_CGraphicContext& gc)
  233. {
  234.     this->MyDrawBackground(ev, gc);
  235.     this->MyDrawPictures(ev, gc);
  236.     this->MyDrawShapes(ev, gc);
  237.     this->MyDrawLine(ev, gc);
  238.     this->MyDrawText(ev, gc);
  239.     this->MyDrawPolygon(ev, gc);
  240. }
  241.  
  242. //----------------------------------------------------------------------------------------
  243. void 
  244. CGraphicsBfrFrame::MyDrawToBitmap(Environment *ev, ODShape* invalidShape)
  245. {
  246.     if (fDisplayChanged) {
  247.         FW_CBitmapContext context(ev, fBitmap);
  248.         context.SetClip(invalidShape);
  249.         this->MyDrawAllStuff(ev, context);
  250.         fDisplayChanged = FALSE;
  251.         }
  252. }
  253.  
  254. //----------------------------------------------------------------------------------------
  255. void 
  256. CGraphicsBfrFrame::FrameShapeChanged(Environment* ev)    // override
  257. {
  258.     FW_CFrame::FrameShapeChanged(ev);
  259.     fDisplayChanged = TRUE;
  260.     this->MyUpdateBitmap(ev);
  261. }
  262.  
  263. //----------------------------------------------------------------------------------------
  264. void 
  265. CGraphicsBfrFrame::MyUpdateBitmap(Environment* ev)
  266. {
  267.     fFrameRect = this->GetBounds(ev);        // Get new frame rect
  268.     const short kPixelSize = 0;
  269.     const FW_Boolean kScaleBitmap = true;
  270.     fBitmap.ChangeBitmap(    FW_FixedToInt(fFrameRect.Width()), 
  271.                             FW_FixedToInt(fFrameRect.Height()),
  272.                             kPixelSize,
  273.                             kScaleBitmap);
  274.     if (fBitmapShape)
  275.         delete fBitmapShape;
  276.     fBitmapShape = FW_NEW(FW_CBitmapShape, (fBitmap, fFrameRect));
  277.     FW_CAcquiredODShape invalidShape = ::FW_NewODShape(ev, fFrameRect);
  278.     this->MyDrawToBitmap(ev, invalidShape);
  279. }
  280.  
  281. //------------------------------------------------------------------------------
  282. void 
  283. CGraphicsBfrFrame::MyDrawShapes(Environment* ev, FW_CGraphicContext& gc)
  284. {
  285.     FW_CRect         boxRect(FW_IntToFixed(25), FW_IntToFixed (25), FW_IntToFixed(100), FW_IntToFixed(100));
  286.     
  287.     // draw filled black box
  288.     FW_CRectShape     boxShape(boxRect, FW_kFill);
  289.     boxShape.Render(gc);
  290.  
  291.     // draw filled green box
  292.     boxShape.MoveShape(FW_IntToFixed(25), FW_IntToFixed(25));
  293.     boxShape.GetInk().SetForeColor(FW_kRGBGreen);
  294.     boxShape.Render(gc);
  295.     
  296.     // draw framed black box
  297.     boxRect.Offset(FW_IntToFixed(50), FW_IntToFixed(50));
  298.     FW_CRectShape::RenderRect(gc, boxRect, FW_kFrame);
  299.     
  300.     // draw oval inscribed in box; special pattern, transfer mode
  301.     boxRect.Offset(FW_IntToFixed(25), FW_IntToFixed(25));
  302.     const FW_CColor kForeColor(FW_kRGBRed);
  303.     const FW_CColor kBackColor(FW_kRGBBlue);
  304.     FW_CInk ink(kForeColor, kBackColor, FW_kXOr);
  305.     const FW_Fixed kPenSize = FW_IntToFixed(4);
  306.     const FW_CPattern kPattern = FW_kDiagCrossPat;
  307.     FW_CStyle style(kPenSize, kPattern);
  308.     FW_COvalShape::RenderOval(gc, boxRect, FW_kFill, ink, style);
  309.  
  310.     // draw oval frame
  311.     FW_COvalShape::RenderOval(gc, boxRect, FW_kFrame);
  312.     
  313.     // draw arrow
  314.     fRegionShape->Render(gc);
  315. }
  316.  
  317. //------------------------------------------------------------------------------
  318. void 
  319. CGraphicsBfrFrame::MyDrawText(Environment* ev, FW_CGraphicContext& gc)
  320. {
  321.     FW_CString32 string("ODF");
  322.     FW_CPoint position( FW_IntToFixed(200), FW_IntToFixed(50) );
  323.  
  324.     // draw 12 point ODF
  325.     FW_CTextShape stringShape(string, position.x, position.y);
  326.     stringShape.Render(gc);
  327.  
  328.     // draw fancy ODF
  329.     const FW_Fixed kFontSize = FW_IntToFixed(48);
  330.     FW_CTextShape::RenderText(gc, 
  331.                             string, 
  332.                             position,
  333.                               FW_CFont(FW_kPalatino, FW_kItalic, kFontSize), 
  334.                               FW_kTextAlignLeft | FW_kTextAlignBottom,
  335.                               FW_CInk(FW_kRGBBlue, FW_kRGBWhite, FW_kOr));
  336. }
  337.  
  338. //------------------------------------------------------------------------------
  339. void 
  340. CGraphicsBfrFrame::MyDrawPictures(Environment* ev, FW_CGraphicContext& gc)
  341. {
  342.     FW_CSharedLibraryResourceFile resFile(ev);    // resource from our shared library
  343.  
  344.     fLargePictureShape->Render(gc);
  345.     
  346.     // draw DevU logo - I choose size
  347.     const short kDevUPictID = 2000;
  348.     const short kCatPictID = 2002;
  349.     const short kOPFIconPictID = 2003;
  350.      FW_CPictureShape::RenderPicture(gc,
  351.                                     FW_CPicture(resFile, kDevUPictID),
  352.                                     FW_CRect(FW_IntToFixed(25), FW_IntToFixed(175), FW_IntToFixed(125), FW_IntToFixed(275))
  353.                                     );
  354. }
  355.  
  356. //------------------------------------------------------------------------------
  357. void 
  358. CGraphicsBfrFrame::MyDrawLine(Environment* ev, FW_CGraphicContext& gc)
  359. {
  360.     fLineShape->Render(gc);
  361. }
  362.  
  363. //------------------------------------------------------------------------------
  364. void 
  365. CGraphicsBfrFrame::MyDrawPolygon(Environment* ev, FW_CGraphicContext& gc)
  366. {
  367.     fPolygonShape->Render(gc);
  368. }
  369.  
  370. //----------------------------------------------------------------------------------------
  371. FW_Boolean 
  372. CGraphicsBfrFrame::DoMouseDown(Environment* ev, const FW_CMouseEvent& theMouseEvent)
  373. {    
  374.     FW_Boolean eventHandled = FALSE;
  375.     FW_CPoint where = theMouseEvent.GetMousePosition(ev, FW_CMouseEvent::kFrame);
  376.     this->GetContentView(ev)->FrameToViewContent(ev, where);
  377.     ODFacet* theFacet = theMouseEvent.GetFacet(ev);
  378.     FW_CViewContext context(ev, this, theFacet, NULL);    // invalidShape = NULL
  379.     const FW_Fixed kTolerance = FW_IntToFixed(2);
  380.     if ( fLineShape->HitTest(context, where, kTolerance) ) {            // new argument for d9
  381.         FW_Beep();
  382.         eventHandled = TRUE;
  383.         this->MyInvalidateLine(ev);
  384.         fLineShape->MoveShape(FW_IntToFixed(0), FW_IntToFixed(20));        // move down
  385.         this->MyInvalidateLine(ev);    
  386.         fDisplayChanged = true;    
  387.         }    // if
  388.  
  389.     if ( fRegionShape->HitTest(context, where, kTolerance) ) {
  390.         FW_Beep();
  391.         eventHandled = true;
  392.         this->Invalidate(ev, fRegionShape->GetODShape() );    // won't blink when buffered
  393.         }    // if
  394.  
  395.     if ( fPolygonShape->HitTest(context, where, kTolerance) ) {
  396.         FW_Beep();
  397.         eventHandled = true;
  398.         }    // if
  399.  
  400.     return eventHandled;
  401. }
  402.  
  403. //------------------------------------------------------------------------------
  404. void 
  405. CGraphicsBfrFrame::MyInvalidateLine(Environment* ev)
  406. {
  407.     FW_CPoint start;
  408.     FW_CPoint end;
  409.     fLineShape->GetGeometry(start, end);
  410.     FW_CRect lineRect(start.x, start.y, end.x, end.y);
  411.     lineRect.Sort();
  412.     FW_Fixed halfPen = fLineShape->GetPenSize() / FW_IntToFixed(2);
  413.     lineRect.Inset(-halfPen, -halfPen);        // expand for pen size
  414.     this->Invalidate(ev, lineRect);            // force update
  415. }
  416.  
  417. //------------------------------------------------------------------------------
  418. FW_CPrintHandler* 
  419. CGraphicsBfrFrame::NewPrintHandler(Environment* ev)
  420. {
  421.     FW_CPart* part = GetPart(ev);
  422.     return new FW_CPrintHandler(part, this);
  423. }
  424.  
  425. //------------------------------------------------------------------------------
  426. FW_Boolean 
  427. CGraphicsBfrFrame::IsCurrentlyPrintable(Environment* ev) const
  428. {
  429.     return TRUE;
  430. }
  431.  
  432. //------------------------------------------------------------------------------
  433. void 
  434. CGraphicsBfrFrame::GetPrintContentExtent(Environment *ev, FW_CPoint& extent) const
  435. {
  436.     extent = fLargePictRect.Size();
  437.     FW_ASSERT(extent.x != FW_kFixed0);
  438.     FW_ASSERT(extent.y != FW_kFixed0);
  439. }
  440.  
  441.